﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class RoomUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtRoom_Alias.Enabled = lbID.Value == "";
            if (!IsPostBack)
            {
                BindGridDropDVT();
                BindGridDropChiNhanh();
                BindGridDropKho();
            }
        }

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertRoom();
            }
            else
            {
                UpdateRoom();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            BindGridDropDVT();
            BindGridDropChiNhanh();
            BindGridDropKho();
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsRoom = new Room_BLL();
            var dtRoom = clsRoom.GetRoom();
            if (dtRoom != null && dtRoom.Rows.Count > 0)
            {
                foreach (DataRow dr in dtRoom.Rows)
                {
                    if (txtRoom_Alias.Text == dr["Room_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã phòng chứa !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertRoom()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Room_ET
                    {
                        Room_Alias = txtRoom_Alias.Text,
                        Room_Name = txtRoom_Name.Text,
                        Room_Tankage = float.Parse(txtRoom_Tankage.Text.Replace(",", "")),
                        Unit_Id = int.Parse(drUnit.SelectedValue),
                        Warehouse_Id = int.Parse(drWarehouse.SelectedValue)
                    };
                    Room_BLL.InsertRoom(obj);
                    ClearTextBox();
                    BindGridDropKho();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtRoom_Alias.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateRoom()
        {
            try
            {
                var obj = new Room_ET
                {
                    Room_Id = int.Parse(lbID.Value),
                    Room_Alias = txtRoom_Alias.Text,
                    Room_Name = txtRoom_Name.Text,
                    Room_Tankage = float.Parse(txtRoom_Tankage.Text.Replace(",", "")),
                    Unit_Id = int.Parse(drUnit.SelectedValue),
                    Warehouse_Id = int.Parse(drWarehouse.SelectedValue)
                };
                Room_BLL.UpdateRoom(obj);
                ((Page.Room)Page).GetRoom();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtRoom_Alias.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        public void BindGridDropDVT()
        {
            var clsUnit = new Unit_BLL();
            var dtUnit = clsUnit.GetUnit();
            if (dtUnit != null && dtUnit.Rows.Count > 0)
            {
                drUnit.Items.Clear();
                drUnit.Items.Add(new ListItem("-Vui lòng chọn đơn vị tính-", "-1"));
                drUnit.DataSource = dtUnit;
                drUnit.DataTextField = "Unit_Name";
                drUnit.DataValueField = "Unit_Id";
                drUnit.DataBind();
            }
        }

        public void BindGridDropChiNhanh()
        {
            var clsBracnch = new Branch_BLL();
            var dtBranch = clsBracnch.GetBranch();
            if (dtBranch != null && dtBranch.Rows.Count > 0)
            {
                drBranch.Items.Clear();
                drBranch.Items.Add(new ListItem("-Vui lòng chọn chi nhánh-", "-1"));
                drBranch.DataSource = dtBranch;
                drBranch.DataValueField = "STT";
                drBranch.DataTextField = "TENCHINHANH";
                drBranch.DataBind();
            }
        }
        public void BindGridDropKho()
        {
            var clsKho = new Warehouse_BLL();
            var dtKho = clsKho.GetWarehouse();
            if (dtKho != null && dtKho.Rows.Count > 0)
            {
                drWarehouse.Items.Clear();
                drWarehouse.Items.Add(new ListItem("-Vui lòng chọn kho-", "-1"));
                drWarehouse.DataSource = dtKho;
                drWarehouse.DataValueField = "Warehouse_Id";
                drWarehouse.DataTextField = "Warehouse_Name";
                drWarehouse.DataBind();
            }
        }
        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtRoom_Alias.Text = string.Empty;
            txtRoom_Name.Text = string.Empty;
            txtRoom_Tankage.Text = string.Empty;
            drUnit.SelectedIndex = 0;
            drBranch.SelectedIndex = 0;
            drWarehouse.SelectedIndex = 0;
        }
        #endregion

        protected void drBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drBranch.SelectedIndex == 0)
            {
                drWarehouse.Enabled = false;
            }
            else
            {
                drWarehouse.Enabled = true;
                var clsKho = new Warehouse_BLL();
                var dtKho = clsKho.GetWarehouseBranch(int.Parse(drBranch.SelectedValue));
                if (dtKho != null && dtKho.Rows.Count > 0)
                {
                    drWarehouse.Items.Clear();
                    drWarehouse.Items.Add(new ListItem("-Vui lòng chọn kho-", "-1"));
                    drWarehouse.DataSource = dtKho;
                    drWarehouse.DataValueField = "Warehouse_Id";
                    drWarehouse.DataTextField = "Warehouse_Name";
                    drWarehouse.DataBind();
                }
                else
                {
                    drWarehouse.Enabled = false;
                    drBranch.SelectedIndex = 0;
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Phòng chứa này không có kho vui lòng chọn phòng chứa khác !";
                    OpenDialog("#thongbao");
                    if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                }
            }
        }
    }
}